package yxy.game.pm2.active.command.vip;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.vipdata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_VIP;
import yxy.game.pm2.active.module.ActiveModule_月卡;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActVipInfoRequest;

@MessageType(proto = Command.ActVipInfo, classtype = ActiveMessage.ActVipInfoRequest.class)
public class CMD_Active_VIP_Info extends Command<ActVipInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, ActVipInfoRequest request) throws Exception {
		String playerCode = messageSource.getTag();

		ActiveMessage.ActVipInfoResponse.Builder response = ActiveMessage.ActVipInfoResponse.newBuilder();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		ActiveModule_VIP am = ActiveModule_VIP.get(playerCode,false);

		int viplv = playerBean.getViplevel();
		long needexp = 0;
		long vipexp = 0;
		vipdata _vipdata = DataCenter.getData(viplv, vipdata.class);
		vipdata _lastvipdata = DataCenter.getData(viplv - 1, vipdata.class);
		if (_lastvipdata != null) {
			vipexp = Currency.number(playerCode, CurrencyType.CT_VIP经验, 0,false);
			vipexp = vipexp - _lastvipdata.getVipexp();
		}
		if (_vipdata != null) {
			needexp = _vipdata.getNeedexp() - vipexp;
		}

		response.setViplv(viplv);
		response.setUpneedexp(needexp);
		response.setVipexp(vipexp);
		response.setVipgoods(am.hadbuyvipgoods().toString());
		response.setCangetmcrvipreward(ActiveModule_月卡.cangetvipmcreward(playerCode));
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActVipInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
